package com.wanxi.dangdang.service.impl;

import com.wanxi.dangdang.common.UserType;
import com.wanxi.dangdang.mapper.WeChatMapper;
import com.wanxi.dangdang.model.pojo.CUser;
import com.wanxi.dangdang.model.vo.CUserCheckVo;
import com.wanxi.dangdang.model.vo.LoginVo;
import com.wanxi.dangdang.service.UserService;
import com.wanxi.dangdang.service.WeChatService;
import com.wanxi.dangdang.util.JwtTokenUtil;
import com.wanxi.dangdang.util.PasswordUtil;
import com.wanxi.dangdang.util.SmsToPhone;
import io.jsonwebtoken.Jwt;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @Author: WangRui
 * @Description:
 * @Date: Created in 2021/9/6 0006
 * @Version: 1.0
 */
@Service
public class WeChatServiceImpl implements WeChatService {
    @Autowired
    private WeChatMapper wechatMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Override
    public HashMap<String, String> loginPassOrRegister(LoginVo loginVo) {
        String token = "";
        CUser cUser = wechatMapper.findByAccount(loginVo.getAccount());
        HashMap<String, String> map = new HashMap<>();
        if (ObjectUtils.isEmpty(cUser)){
            cUser = new CUser();
            cUser.setPhone(loginVo.getAccount());
            cUser.setPicture("/image/slt/01.jpg");
            long timeMillis = System.currentTimeMillis();
            String string = Long.toString(timeMillis);
            cUser.setUsername("c_"+string.substring(3,10));
            cUser.setPassword(passwordEncoder.encode(loginVo.getPassword()));
            wechatMapper.add(cUser);
        }
        if (passwordEncoder.matches(loginVo.getPassword(),cUser.getPassword())) {
            token = jwtTokenUtil.generateToken(cUser.getPhone(), cUser.getId(), UserType.CUSER);
        }else {
            return map;
        }
        map.put("token",tokenHead + " " + token);
        map.put("tokenHeader",tokenHeader);
//        map.put("weChatId",cUser.getId().toString());
        return map;
    }

    @Override
    public HashMap<String, String> loginCode(LoginVo loginVo) {
        String token = "";
        CUser cUser = wechatMapper.findByAccount(loginVo.getAccount());
        HashMap<String, String> map = new HashMap<>();
        if (ObjectUtils.isEmpty(cUser)){
            return map;
        }
        token = jwtTokenUtil.generateToken(cUser.getPhone(), cUser.getId(), UserType.CUSER);
        map.put("token",tokenHead + " " + token);
        map.put("tokenHeader",tokenHeader);
//        map.put("weChatId",cUser.getId().toString());
        return map;
    }

    @Override
    public void getPhoneCode(String phone) throws Exception {
        userService.getPhoneCode(phone);
    }

    @Override
    public CUserCheckVo checkAccount(String account) {
        return wechatMapper.checkAccount(account);
    }

    @Override
    public CUser findById(Long id) {
        return wechatMapper.findById(id);
    }


}
